﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LockCrypt.Core;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A button which shows a context menu when clicked.
    /// </summary>
    public partial class ContextButton : UserControl {
        public delegate void ContextItemDelegate(ContextItemCheckedEventArgs e);
        /// <summary>
        /// Occurs when an item in the context menu is clicked
        /// </summary>
        public event ContextItemDelegate ContextItemChecked;

        /// <summary>
        /// Gets or sets a value indiciating the current state of the text field the button corresponds to.
        /// </summary>
        public FieldMetaData CurrentState { get; set;}

        /// <summary>
        /// Initializes a new ContextButton
        /// </summary>
        /// <param name="currentState">The current state of the text field</param>
        public ContextButton(FieldMetaData currentState) {
            InitializeComponent();
            this.CurrentState = currentState;
            switch(currentState) {
                case FieldMetaData.Password:
                    itmObfuscate.Checked = true;
                    break;
                case FieldMetaData.Phone:
                    itmPhoneNumber.Checked = true;
                    break;
                case FieldMetaData.Url:
                    itmUrl.Checked = true;
                    break;
            }
            button.Click += (s, e) => {
                menu.Show(button, new Point(Width, 0));
            };
            itmPhoneNumber.Click += (s, e) => {
                if(ContextItemChecked != null)
                    ContextItemChecked(new ContextItemCheckedEventArgs(CurrentState) {
                        PhoneNumber = true
                    });
            };
            itmObfuscate.Click += (s, e) => {
                if(ContextItemChecked != null)
                    ContextItemChecked(new ContextItemCheckedEventArgs(CurrentState) {
                        ObfuscateField = true
                    });
            };
            itmUrl.Click += (s, e) => {
                if(ContextItemChecked != null)
                    ContextItemChecked(new ContextItemCheckedEventArgs(CurrentState) {
                        IsUrl = true
                    });
            };
            itmDeleteField.Click += (s, e) => {
                if(ContextItemChecked != null)
                    ContextItemChecked(new ContextItemCheckedEventArgs(CurrentState) {
                        DeleteField = true
                    });
            };
            itmRenameField.Click += (s, e) => {
                if(ContextItemChecked != null)
                    ContextItemChecked(new ContextItemCheckedEventArgs(CurrentState) {
                        RenameField = true
                    });
            };
        }
    }
}
